/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.h2x.transform.position.schema.state;

import juixb.schema.IterationOperator;
import juixb.schema.SchemaNode;

/**
 *
 * @author lendle
 */
public class IterationStateParser implements StateParser{

    public boolean isApplicable(SchemaNode schemaNode) {
        return schemaNode instanceof IterationOperator;
    }

    public State parseState(StateParsers parsers, SchemaNode schemaNode) {
        IterationOperator it=(IterationOperator) schemaNode;
        DefaultPseudoStateImpl state=new DefaultPseudoStateImpl();
        State child=parsers.parseState((SchemaNode)schemaNode.getFirstChild());
        state.addChildState(child);
        state.setMin(it.getMin());
        state.setMax(it.getMax());
        state.setFirstSet(child.getFirstSet());
        state.setLastSet(child.getLastSet());
        if(it.getMax()>1 || it.getMax()==-1){
            Transition transition=new Transition();
            for(State last : child.getLastSet()){
                for(State first : child.getFirstSet()){
                    //System.out.println("\t"+last+", "+first);
                    transition.setQName(((ElementState)first).getElementNode().getQName());
                    transition.setState(first);
                    ((ElementState)last).addTransition(transition);
                }
            }
        }
        return state;
    }
    
}
